<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-left">
        <div class="back-nav">
          <div class="back-button" @click="goBack">
            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
              <path d="M19 12H5M12 19l-7-7 7-7"></path>
            </svg>
            <span>返回</span>
          </div>
        </div>
        <div class="brand">
          <div class="logo">EDU</div>
          <h1>教育共享平台</h1>
          <p class="slogan">让知识触手可及</p>
        </div>
        <div class="decoration">
          <div class="vase-image"></div>
          <div class="stats-card">
            <div class="stat-item">
              <h3>5.8</h3>
            </div>
            <div class="stat-item">
              <h3>99+</h3>
            </div>
            <div class="stat-item">
              <h3>110K</h3>
            </div>
          </div>
        </div>
      </div>
      <div class="login-right">
        <div class="login-header">
          <h2>修改手机号</h2>
          <p>请填写以下信息修改您的手机号</p>
        </div>
        <form class="login-form" @submit.prevent="handleSubmit">
          <div class="form-group">
            <div class="input-group locked-group">
              <div class="input-icon">
                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                  <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path>
                  <circle cx="12" cy="7" r="4"></circle>
                </svg>
              </div>
              <input 
                type="text" 
                v-model="formData.userName" 
                placeholder="用户名"
                disabled
              >
              <div class="locked-hint">
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                  <rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect>
                  <path d="M7 11V7a5 5 0 0 1 10 0v4"></path>
                </svg>
              </div>
            </div>
          </div>

          <div class="form-group">
            <div class="input-group">
              <div class="input-icon">
                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                  <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"></path>
                </svg>
              </div>
              <input 
                type="text" 
                v-model="formData.mobile" 
                placeholder="请输入新手机号"
                @blur="validateMobile"
                required
              >
            </div>
            <span class="error-message" v-if="mobileError">{{ mobileError }}</span>
          </div>

          <div class="form-group">
            <div class="input-group code-group">
              <div class="input-icon">
                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                  <rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect>
                  <path d="M7 11V7a5 5 0 0 1 10 0v4"></path>
                </svg>
              </div>
              <input 
                type="text" 
                v-model="formData.verificationCode" 
                placeholder="请输入验证码"
                required
              >
              <button 
                type="button" 
                class="code-btn" 
                :disabled="isCountDown || !isMobileValid" 
                @click="sendCode"
              >
                {{ countDownText }}
              </button>
            </div>
          </div>

          <div class="form-group">
            <button type="submit" :disabled="loading" class="submit-btn">
              <span class="btn-text">{{ loading ? '提交中...' : '修改手机号' }}</span>
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { getUserName, updateUserPhone, sendResetMessageCode, checkResetMessageCode } from '@/api/user'

export default {
  name: 'PhoneReset',
  
  setup() {
    // 路由实例
    const router = useRouter()
    
    // 表单数据
    const formData = reactive({
      userName: '',    // 用户名
      mobile: '',      // 新手机号
      verificationCode: '' // 验证码
    })

    // 状态变量
    const loading = ref(false)          // 提交按钮加载状态
    const isCountDown = ref(false)      // 验证码倒计时状态
    const countDownText = ref('获取验证码') // 验证码按钮文本
    const mobileError = ref('')         // 手机号错误信息
    const isMobileValid = ref(false)    // 手机号验证状态

    // 组件挂载时获取用户名
    onMounted(async () => {
      try {
        const response = await getUserName()
        if (response.code === 200) {
          formData.userName = response.data.userName
        } else {
          ElMessage.error('获取用户名失败: ' + (response.message || '未知错误'))
        }
      } catch (error) {
        ElMessage.error('获取用户名失败，请稍后重试')
      }
    })

    // 验证手机号格式
    const validateMobile = () => {
      const mobileRegex = /^1[3-9]\d{9}$/
      if (!formData.mobile) {
        mobileError.value = '请输入手机号'
        isMobileValid.value = false
      } else if (!mobileRegex.test(formData.mobile)) {
        mobileError.value = '请输入正确的手机号'
        isMobileValid.value = false
      } else {
        mobileError.value = ''
        isMobileValid.value = true
      }
    }

    // 开始验证码倒计时
    const startCountDown = () => {
      isCountDown.value = true
      let count = 60
      countDownText.value = `${count}秒后重试`
      const timer = setInterval(() => {
        count--
        countDownText.value = `${count}秒后重试`
        if (count <= 0) {
          clearInterval(timer)
          isCountDown.value = false
          countDownText.value = '获取验证码'
        }
      }, 1000)
    }

    // 发送验证码
    const sendCode = async () => {
      try {
        validateMobile()
        if (!isMobileValid.value) return

        const response = await sendResetMessageCode(formData.mobile)
        
        if (response.code === 200) {
          ElMessage.success('验证码已发送')
          startCountDown()
        } else {
          ElMessage.error(response.message || '验证码发送失败')
        }
      } catch (error) {
        ElMessage.error('发送验证码失败，请稍后重试')
      }
    }

    // 提交表单
    const handleSubmit = async () => {
      try {
        validateMobile()
        if (!isMobileValid.value) return

        loading.value = true
        
        // 验证验证码
        const checkResponse = await checkResetMessageCode(formData.mobile, formData.verificationCode)

        if (checkResponse.code !== 200) {
          ElMessage.error('验证码错误')
          loading.value = false
          return
        }

        // 提交修改手机号请求
        const response = await updateUserPhone({
          userName: formData.userName,
          mobile: formData.mobile
        })
        
        if (response.code === 200) {
          ElMessage.success('手机号修改成功')
          router.push('/user')
        } else {
          ElMessage.error(response.message || '手机号修改失败')
        }
      } catch (error) {
        ElMessage.error('修改手机号失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }

    // 返回上一页
    const goBack = () => {
      router.push('/user')
    }

    return {
      formData,
      loading,
      isCountDown,
      countDownText,
      mobileError,
      isMobileValid,
      validateMobile,
      sendCode,
      handleSubmit,
      goBack
    }
  }
}
</script>

<style scoped>
/* 登录页面容器 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #c8cbd2;
  padding: 20px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow-y: auto;
}

/* 登录框容器 */
.login-box {
  display: flex;
  background: #fff;
  border-radius: 24px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.05);
  width: 1200px;
  min-height: 700px;
  overflow: hidden;
}

/* 左侧区域样式 */
.login-left {
  flex: 1.2;
  background: #f8f9fa;
  padding: 60px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
}

/* 返回导航栏 */
.back-nav {
  margin-bottom: 2rem;
}

/* 返回按钮样式 */
.back-button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 20px;
  background: #f0f0f0;
  cursor: pointer;
  transition: all 0.3s;
  color: #333;
  font-weight: 500;
  font-size: 14px;
}

.back-button:hover {
  background: #e0e0e0;
}

.back-button svg {
  stroke-width: 2.5;
}

/* 品牌标识区域 */
.brand {
  position: relative;
  margin-bottom: 2rem;
}

.brand .logo {
  font-size: 2rem;
  font-weight: bold;
  color: #343a40;
  margin-bottom: 1rem;
}

.brand h1 {
  font-size: 2rem;
  color: #343a40;
  margin-bottom: 0.5rem;
}

.brand .slogan {
  color: #6b7280;
  font-size: 1.1rem;
}

/* 装饰区域 */
.decoration {
  position: relative;
  height: 400px;
  margin-top: 40px;
}

.vase-image {
  width: 200px;
  height: 200px;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="%23e9ecef"/></svg>') center/contain no-repeat;
  position: absolute;
  left: 40px;
  bottom: 100px;
}

.stats-card {
  position: absolute;
  right: 20px;
  bottom: 40px;
  background: white;
  padding: 30px;
  border-radius: 24px;
  display: flex;
  gap: 40px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
}

.stat-item h3 {
  color: #343a40;
  font-size: 1.5rem;
  font-weight: 600;
}

/* 右侧登录表单区域 */
.login-right {
  flex: 1;
  padding: 60px 80px;
  background: white;
}

.login-header {
  margin-bottom: 3rem;
}

.login-header h2 {
  color: #1f2937;
  font-size: 2.5rem;
  margin-bottom: 1rem;
  font-weight: 600;
}

.login-header p {
  color: #6b7280;
  font-size: 1.1rem;
}

/* 表单组样式 */
.form-group {
  margin-bottom: 1.5rem;
}

/* 输入框组样式 */
.input-group {
  position: relative;
  display: flex;
  align-items: center;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  transition: all 0.3s ease;
  overflow: hidden;
}

.input-group:focus-within {
  border-color: #1890ff;
  box-shadow: 0 0 0 3px rgba(24, 144, 255, 0.1);
}

.input-icon {
  padding: 0 1rem;
  color: #9ca3af;
}

input {
  width: 100%;
  padding: 0.75rem;
  background: transparent;
  border: none;
  font-size: 1rem;
  color: #1f2937;
  outline: none;
}

input::placeholder {
  color: #9ca3af;
}

/* 验证码输入组样式 */
.code-group {
  display: flex;
  align-items: stretch;
  position: relative;
}

.code-group input {
  padding-left: 4rem;
  flex: 1;
}

.code-group .input-icon {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  padding: 0 1rem;
  color: #9ca3af;
  display: flex;
  align-items: center;
  pointer-events: none;
}

.code-group .input-icon svg {
  margin-top: 2px;
}

/* 验证码按钮样式 */
.code-btn {
  min-width: 120px;
  padding: 0.75rem 1rem;
  background: #f3f4f6;
  border: none;
  border-left: 2px solid #e5e7eb;
  color: #1890ff;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 0 10px 10px 0;
}

.code-btn:hover:not(:disabled) {
  background: #e5e7eb;
}

.code-btn:disabled {
  color: #9ca3af;
  cursor: not-allowed;
}

/* 提交按钮样式 */
.submit-btn {
  width: 100%;
  padding: 1rem;
  background: #343a40;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 2rem;
}

.submit-btn:hover:not(:disabled) {
  background: #212529;
}

.submit-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

/* 错误消息样式 */
.error-message {
  color: #dc2626;
  font-size: 0.875rem;
  margin-top: 0.5rem;
  display: block;
}

/* 响应式布局 - 平板 */
@media (max-width: 1024px) {
  .login-box {
    width: 800px;
  }
  
  .login-right {
    padding: 40px;
  }
}

/* 响应式布局 - 手机 */
@media (max-width: 768px) {
  .login-left {
    display: none;
  }
  
  .login-box {
    width: 100%;
    max-width: 500px;
  }
}

/* 禁用输入框样式 */
.locked-group {
  background-color: #f7f8fa;
  border-color: #e5e7eb;
}

.locked-group input {
  color: #666;
  cursor: not-allowed;
}

.locked-hint {
  margin-right: 1rem;
  color: #9ca3af;
}
</style> 